/*
 * Copyright 2007-2009 Perttu "celeron55" Ahola <celeron55@gmail.com>
 *
 * This file is part of jukumagic.
 *
 * jukumagic is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jukumagic is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with jukumagic.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "common.h"
#include "nettable.h"

NetTable::NetTable(Netlist *netlist)
{
	SetNetlist(netlist);
}

void NetTable::SetNetlist(Netlist *netlist)
{
	m_netlist = netlist;
	
	if(GetView()){
		wxGridTableMessage msg1(this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, 0,
				GetView()->GetNumberRows());
		GetView()->ProcessTableMessage(msg1);
		
		if(m_netlist){
			wxGridTableMessage msg2(this, wxGRIDTABLE_NOTIFY_ROWS_INSERTED, 0,
					m_netlist->m_nets.size());
			GetView()->ProcessTableMessage(msg2);
		}
	}
}

int NetTable::GetNumberRows()
{
	if(!m_netlist) return 0;
	return m_netlist->m_nets.size();
}

int NetTable::GetNumberCols()
{
	return 2;
}

wxString NetTable::GetValue(int row, int col)
{
	if(!m_netlist) return wxT("invalid netlist");
	if(row >= (int)m_netlist->m_nets.size()) return wxT("invalid row");
	if(col==0) return m_netlist->m_nets[row]->m_name;
	if(col==1){
		switch(m_netlist->m_nets[row]->m_validity){
		case NET_VALID:
			return wxT("valid");
		case NET_LACKS_PINS:
			return wxT("lacks pins");
		case NET_INVALID_PIN:
			return wxT("invalid pin");
		}
	}
	return wxT("---");
}

void NetTable::SetValue(int row, int col, const wxString &s)
{
	if(!m_netlist) return;
	if(row >= (int)m_netlist->m_nets.size()) return;
	if(col==0) m_netlist->m_nets[row]->m_name = s;
}

bool NetTable::IsEmptyCell(int row, int col)
{
	if(!m_netlist) return wxT("invalid netlist");
	if(row >= (int)m_netlist->m_nets.size()) return wxT("invalid row");
	if(col==0) return m_netlist->m_nets[row]->m_name.IsEmpty();
	if(col==1) return false;
	return true;
}

wxString NetTable::GetColLabelValue(int col)
{
	if(col==0) return wxT("name");
	if(col==1) return wxT("validty");
	return wxT("");
}

